package com.group.view;

import com.group.controller.StudentController;
import com.group.model.Student;

import javax.swing.*;
import java.awt.*;

/**
 * @author lenovo
 */
public class DeleteStudentPanel extends JPanel {
    private JTextField numField = new JTextField(15);
    private JTextArea resultArea = new JTextArea();
    //显示是否成功
    private JButton deleteBtn = new JButton("删除");
    public DeleteStudentPanel() {
        setLayout(new BorderLayout());

        // 输入框面板
        JPanel inputPanel = new JPanel();
        inputPanel.add(new JLabel("学生学号："));
        inputPanel.add(numField);
        inputPanel.add(deleteBtn);

        // 文本区域只读
        resultArea.setEditable(false);

        add(inputPanel, BorderLayout.NORTH);
        //输入面板放在北部
        add(new JScrollPane(resultArea), BorderLayout.CENTER);
        //输出面板放入中部

        // 事件
        deleteBtn.addActionListener(e -> {
            String num = numField.getText().trim();
            Student stu=new Student();
            if (num.isEmpty()) {
                resultArea.setText("请输入学生学号");
                return;
            }

            boolean found = false;

            for (Student s : StudentController.getAll()) {
                if (s.getId().equals(num)) {  // 精确匹配
                    found = true;
                    stu=s;
                    break;
                }
            }

            if (!found) {
                resultArea.setText("未找到该学生，请确认学号输入是否正确。");
            } else {
                StudentController.deleteStudent(stu);
                resultArea.setText("删除成功");
            }

        });
    }
}
